﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineMovieTicket.Services.IServices;
using OnlineMovieTicket.Services.Services;
using OnlineMovieTicket.Model;
using OnlineMovieTicket.ViewModels;

namespace OnlineMovieTicket.Controllers
{
    public class MoviesController : Controller
    {
        //
        // GET: /Movies/
        IMoviesService movieSrv;

        public MoviesController()
        {
            movieSrv = new MoviesService();
        }

        [Authorize]
        public ActionResult Index()
        {
           IList<Model.Movies> movielist = movieSrv.GetMovies();
            return View(movielist);
        }

        [Authorize]
        public ActionResult Create()
        {
            ViewData["MovieList"] = new SelectList(movieSrv.GetMovies(), "MovieCode", "MovieName");
            
            List<MovieStatusModel> moviestatusList =  new List<MovieStatusModel>();
            moviestatusList.Add(new MovieStatusModel { Status = "N", StatusDescription = "Now Showing" });
            moviestatusList.Add(new MovieStatusModel { Status = "C", StatusDescription = "Commong Soon" });
            moviestatusList.Add(new MovieStatusModel { Status = "P", StatusDescription = "Past" });
            ViewData["MovieStatus"] = new SelectList(moviestatusList, "Status", "StatusDescription");

            Model.Movies movie = new Model.Movies();
            movie.MovieCode = movieSrv.GetNewMovieCode();
            


            return View(movie);
        }

        [HttpPost]        
        public ActionResult Create(FormCollection collection)
        {
            if (ModelState.IsValid)
            {

                Model.Movies movie = new Model.Movies();
                
                var file = Request.Files[0];
                if (file != null)
                {
                    if (file.ContentLength > 0)
                    {
                        byte[] buf = new byte[file.ContentLength];
                        file.InputStream.Read(buf, 0, file.ContentLength);
                        movie.Image = buf;                        
                    }
                    
                }
                TryUpdateModel(movie);
                movieSrv.Save(movie, 0);
            }
            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult Edit(string id)
        {
            List<MovieStatusModel> moviestatusList = new List<MovieStatusModel>();
            moviestatusList.Add(new MovieStatusModel { Status = "N", StatusDescription = "Now Showing" });
            moviestatusList.Add(new MovieStatusModel { Status = "C", StatusDescription = "Commong Soon" });
            moviestatusList.Add(new MovieStatusModel { Status = "P", StatusDescription = "Past" });
            ViewData["MovieStatus"] = new SelectList(moviestatusList, "Status", "StatusDescription");

            
            Movies movie = movieSrv.GetMovie(int.Parse(id));
            return View(movie);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Edit(string id, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                Model.Movies movie = new Model.Movies();
                UpdateModel(movie);               

                var file = Request.Files[0];
                if (file != null)
                {
                    if (file.ContentLength > 0)
                    {
                        byte[] buf = new byte[file.ContentLength];
                        file.InputStream.Read(buf, 0, file.ContentLength);
                        movie.Image = buf;
                    }                  
                }
               
                movieSrv.Save(movie, 1);
            }
            return RedirectToAction("Index");
        }
                
        public ActionResult Delete(int id)
        {
            IMoviesService movieSrv = new MoviesService();
            bool bResult = movieSrv.Delete(id);
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ShowImage(string moviecode)
        {

            Movies movie = movieSrv.GetMovie(Convert.ToInt32(moviecode));
            
            ImageResult returnresult = new ImageResult(movie.Image, "image/jpeg");
            return returnresult;
        }
    }
}
